﻿using System;
using System.Linq;
using System.Web.Mvc;
using Citi.EmailSender.Domain;
using Citi.EmailSender.WebUi.Models;

namespace Citi.EmailSender.WebUi.Controllers
{
    public class ApprovalController : ControllerBase
    {
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            
        }

        public ActionResult MyApprovals()
        {
            return View();
        }

        public ActionResult MyList(bool? approved = null)
        {
            //var user = (User)Session["LoggedUser"];
            var emails = new Email().GetEmailByUserApproval(HttpContext.User.Identity.Name.Split('\\').Last(), approved);

            if (approved.HasValue)
                emails = emails.Take(30).ToList();

            return PartialView(new EmailModel().CreateModel(emails));
        }

        public ActionResult Approve(int id)
        {
            return SetApproval(id, true);
        }

        private ActionResult SetApproval(int id, bool approved)
        {
            var email = new Email().SelectById<Email>(id);
            email.Approved = approved;
            email.Save(email);

            return RedirectToAction("MyApprovals");
        }

        public ActionResult Disapprove(int id)
        {
            return SetApproval(id, false);
        }
    }
}
